﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class UnitControllerBase : UnitComponentBase
{
    private Dictionary<Type, IDictionary> m_StreamDict = new Dictionary<Type, IDictionary>();
    /// <summary>
    /// 注册输入流
    /// </summary>
    public void RegisterStream<T>(string streamName, IObservable<T> stream)
    {
        if (streamName.IsEmpty())
        {
            Debug.LogError($"输入流注册错误：输入流名称为空");
            return;
        }
        if (stream == null)
        {
            Debug.LogError($"输入流注册错误：输入流 {streamName} 的值为空");
            return;
        }
        var type = typeof(T);
        if (!m_StreamDict.TryGetValue(type, out var idict))
        {
            idict = new Dictionary<string, IObservable<T>>();
            m_StreamDict.Add(type, idict);
        }
        var dict = idict as Dictionary<string, IObservable<T>>;
        if (dict.ContainsKey(streamName))
        {
            Debug.LogError($"输入流注册错误：输入流 {streamName} 已注册，请勿重复注册");
            return;
        }
        dict[streamName] = stream.WhenActive(entity);
    }

    /// <summary>
    /// 获取输入流
    /// </summary>
    public IObservable<T> GetStream<T>(string streamName)
    {
        var type = typeof(T);
        if (!m_StreamDict.TryGetValue(type, out var idict))
        {
            Debug.LogError($"输入流获取失败：输入流 {streamName} 不存在");
            return null;
        }
        var dict = idict as Dictionary<string, IObservable<T>>;
        if (!dict.TryGetValue(streamName, out var stream))
        {
            Debug.LogError($"输入流获取失败：输入流 {streamName} 不存在");
        }
        return stream;
    }
}